package com.shop.dao.impl;

import java.io.Serializable;
import java.util.List;




import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shop.dao.UserDao;
import com.shop.entity.User;
@Repository
@Transactional
public class UserDaoImpl extends HibernateTemplate implements UserDao{
	
	@Autowired
	private SessionFactory sessionFactory;
	@Override
	public Serializable saveUser(User user) {
		return super.save(user);
	}

	@Override
	public Serializable deleteUser(User user) {
		super.delete(user);
		return null;
	}

	@Override
	public Serializable updateUser(User user) {
		super.update(user);
		return null;
	}

	@Override
	public List<User> findUser(User user) {
		List<User> list = (List<User>)super.find("from User");
		return list;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

//	@Override
//	public User login(String name, String password) {
//		String hql ="from user where user_name=? and user_password=? ";
//		Session session = sessionFactory.openSession();
//		Query q = session.createQuery(hql);
//		q.setString(1,name);
//		return null;
//	}

	@Override
	public User login(User user) {
		String hql ="from User where user_name=? and user_password=? ";
		//String hql ="from User where user_name=:u and user_password=? ";
		Session session = sessionFactory.openSession();
		Query q = session.createQuery(hql);
		
		
	//	q.setString("u", user.getUserName());
		q.setString(0, user.getUserName());
		q.setString(1, user.getUserPassword());
		List<User> list = q.list();//q.uniqueResult();
	//	System.out.println("list大小：:::::::::::::"+list.size());
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	

}
